package com.egreen.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.egreen.common.pojo.DTO.system.AddCommodityDTO;
import com.egreen.common.pojo.DTO.system.ChangePriceDTO;
import com.egreen.common.pojo.DTO.system.ReduceStockDTO;
import com.egreen.common.pojo.entity.system.Commodity;
import com.egreen.system.dao.CommodityDao;
import com.egreen.system.ex.SystemException;
import com.egreen.system.ex.ValidException;
import com.egreen.system.service.ICommodityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cloudJiang
 * @since 2022-07-16
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityDao, Commodity> implements ICommodityService {
    @Autowired
    ICommodityService commodityService;
    @Autowired
    CommodityDao commodityDao;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void addCommodity(AddCommodityDTO addCommodityDTO) {
        Commodity commodity = new Commodity();
        commodity.setName(addCommodityDTO.getName());
        commodity.setPrice(addCommodityDTO.getPrice());
        commodity.setRemainingQuantity(addCommodityDTO.getRemainingQuantity());
        commodity.setUrl(addCommodityDTO.getUrl());
        commodity.setLaunchTime(LocalDateTime.now());
        commodityService.save(commodity);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void deleteCommodity(Integer commodityID) {
        commodityService.removeById(commodityID);
    }

    @Override
    public void reduceStock(ReduceStockDTO reduceStockDTO) {
        if (reduceStockDTO.getRemainingQuantity() < 0) {
            throw new ValidException("参数不合法");
        }

        Commodity commodity = commodityDao.selectById(reduceStockDTO.getId());
        commodity.setRemainingQuantity(reduceStockDTO.getRemainingQuantity());
        commodityDao.updateById(commodity);
//        UpdateWrapper<Commodity> updateWrapper = new UpdateWrapper<>();
//        updateWrapper.eq("id", reduceStockDTO.getId());
//        Commodity change = new Commodity();
//        change.setRemainingQuantity(reduceStockDTO.getRemainingQuantity());
//        commodityDao.update(change, updateWrapper);
    }

    @Override
    public void changePrice(ChangePriceDTO changePriceDTO) {
        if (changePriceDTO.getPrice() < 0) {
            throw new ValidException("参数不合法");
        }
        UpdateWrapper<Commodity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", changePriceDTO.getId());
        Commodity change = new Commodity();
        change.setPrice(changePriceDTO.getPrice());
        commodityDao.update(change, updateWrapper);
    }

    @Override
    public void addMerchandise(AddCommodityDTO addCommodityDTO) {
        Commodity commodity = new Commodity();
        BeanUtils.copyProperties(addCommodityDTO, commodity);
        int insert = commodityDao.insert(commodity);
        if (insert != 1) {
            throw new SystemException("增加失败");
        }
    }

    @Override
    public void deleteMerchandise(int id) {
        if (id <= 0) {
            throw new SystemException("参数错误");
        }
        int delete = commodityDao.deleteById(id);
        if (delete != 1) {
            throw new SystemException("删除失败");
        }
    }
}
